package com.example.maybeaccount.Entry;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.maybeaccount.MainActivity;
import com.example.maybeaccount.Mode.AlarmService;
import com.example.maybeaccount.Mode.EncrypDES;
import com.example.maybeaccount.Mode.Item;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;

import java.util.ArrayList;
import java.util.List;


public class Entrance extends AppCompatActivity /*implements LockPatternView.OnPatternChangeListener*/ {
    /**
     * @author ghc
     * date 2020/10/9
     * 程序入口，用于确定用户选择登录方式以及判断是否注册
     */
    private List<User> userList = new ArrayList<>();
    private User user;
    private String resumeFlag ; //从后台唤醒的标记
    private Item item;
    private Intent intent;
    private int biometricBack;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entrance);
        Button butTextPswd = findViewById(R.id.textPswd);
        Button butGraphPswd = findViewById(R.id.graphPswd);
        Button butFingerPswd = findViewById(R.id.fingerPswd);
        TextView texUserName = findViewById(R.id.userName);

        if(userCheck()){
            //快速记账处理
            String shortFlag = getIntent().getStringExtra("shortflag");
            if(shortFlag != null && shortFlag.equals("short")){
                intent = new Intent(Entrance.this, NineGridUnlock.class);
                intent.putExtra("shortflag","short");
                startActivity(intent);
                finish();
            }else {
                //userList = SqliteDB.getInstance(getApplicationContext()).loadUser();
                //user = userList.get(0);
                try {
                    EncrypDES encrypDES = new EncrypDES();
                    texUserName.setText(encrypDES.decrypt(user.getUsername()));
                }catch (Exception e){
                    Log.d("错误",e.getMessage());
                }


                //处理后台唤醒和正常启动
                intent = getIntent();
                if(intent == null){

                } else {
                    resumeFlag = intent.getStringExtra("resume");
                    item = (Item)intent.getSerializableExtra("item");
                }

                if(user.getDefaultUnlockMethod() == User.UNLOCK_METHOD_ALL){

                }else if(user.getDefaultUnlockMethod() == User.UNLOCK_METHOD_TEXT){
                    intent = new Intent(Entrance.this, LoginActivity.class);
                    intent.putExtra("resume",resumeFlag);
                    intent.putExtra("item",item);
                    startActivity(intent);
                    finish();
                }else if(user.getDefaultUnlockMethod() == User.UNLOCK_METHOD_GRID){
                    intent = new Intent(Entrance.this, NineGridUnlock.class);
                    intent.putExtra("resume",resumeFlag);
                    intent.putExtra("item",item);
                    startActivity(intent);
                    finish();
                }else if(user.getDefaultUnlockMethod() == User.UNLOCK_METHOD_BIO){
                    BiometricPromptHelper biometricPromptHelper = new BiometricPromptHelper();
                    biometricPromptHelper.init(Entrance.this);
                    biometricBack = biometricPromptHelper.Getback();
                    if (biometricBack == 1){
                        Intent intent = new Intent(Entrance.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        }
        butTextPswd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(Entrance.this, LoginActivity.class);
                intent.putExtra("resume",resumeFlag);
                intent.putExtra("item",item);
                startActivity(intent);
                finish();
            }
        });

        butGraphPswd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(user.getGridPwd().equals("00000")){
                    Toast.makeText(getApplicationContext(),"您还未设置图形密码",Toast.LENGTH_LONG).show();
                    return;
                }
                intent = new Intent(Entrance.this, NineGridUnlock.class);
                intent.putExtra("resume",resumeFlag);
                intent.putExtra("item",item);
                startActivity(intent);
                finish();
            }
        });

        butFingerPswd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BiometricPromptHelper biometricPromptHelper = new BiometricPromptHelper();
                biometricPromptHelper.init(Entrance.this);
                biometricBack = biometricPromptHelper.Getback();
                if (biometricBack == 1){
                    Intent intent = new Intent(Entrance.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    /**
     * 重写停止活动，在停止时开启提醒服务
     */
    @Override
    public void onPause(){
        super.onPause();
        Intent intent = new Intent(this, AlarmService.class);
        startService(intent);
    }


    /**
     * 重写返回键 直接退出程序
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
        }
        return true;
    }

    private boolean userCheck(){
        //判断是否需要注册密码
        //若需要则跳至页面，否则直接返回
        userList = SqliteDB.getInstance(getApplicationContext()).loadUser();
        if(userList.isEmpty()){
            Intent intent = new Intent(Entrance.this, Register.class);
            //startActivityForResult(intent,1);
            startActivity(intent);
            return false;
        }else{
            this.user = userList.get(0);
            return true;
        }
    }

}
